共计 1345 个字符,预计需要花费 4 分钟才能阅读完成。
基本类型的多维数组,可以使用‘{}’将每个向量分隔开:
/**
* @author plm
* @create 2021/1/10 19:50
*/
public class MultimensionalPrimitiveArrayDemo {
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(Arrays.deepToString(arr)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
double[][][] doubles = new double[3][1][2];
System.out.println(Arrays.deepToString(doubles)); // [[[0.0, 0.0]], [[0.0, 0.0]], [[0.0, 0.0]]]
String[][][] strings = new String[2][2][2];
System.out.println(Arrays.deepToString(strings)); // [[[null, null], [null, null]], [[null, null], [null, null]]]
}
}
基本类型数组的值,在不进行显示初始化时,会被自动初始化;
对象数组会被初始化为null;
数组中构成矩阵的每个向量都可以具有任意的长度(称为粗糙数组):
/**
* @author plm
* @create 2021/1/10 19:50
*/
public class MultimensionalPrimitiveArrayDemo {
public static void main(String[] args) {
Random random = new Random(47);
int[][][] arrs = new int[random.nextInt(10)][][];
for (int i = 0; i < arrs.length; i++) {
arrs[i] = new int[random.nextInt(10)][];
for (int j = 0; j < arrs[i].length; j++) {
arrs[i][j] = new int[random.nextInt(10)];
}
}
System.out.println(Arrays.deepToString(arrs));
// [[[0, 0, 0], [0], [0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0], [], [0, 0, 0, 0, 0, 0, 0, 0]], [[], [0], [0, 0], [0, 0, 0, 0], [0, 0, 0], [0, 0, 0, 0, 0, 0]], [[], [0, 0], [0, 0, 0, 0], [0, 0, 0, 0], []], [[0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], []], [[0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0]]]
}
}
正文完